<?php

/**
 * JXHEditor class file.
 *
 * @author jerry2801 <jerry2801@gmail.com>
 * @version alpha 2
 *
 * A typical usage of JXHEditor is as follows:
 * <pre>
 * $this->widget('application.extensions.xheditor.JXHEditor', array(
 *     'model' => $model,
 *     'attribute' => 'intro',
 *     'options' => array(
 *         'width' => '100%',
 *         'height' => 400,
 *     ),
 * ));
 * </pre>
 */

class JXHEditor extends CWidget
{
    public $model;
    public $attribute;
	public $baseUrl;
    public $language = 'zh-cn';
    public $options = array();
    public $htmlOptions = array();

    public function init()
    {
        $options = CJavaScript::jsonEncode($this->options);

        $dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'source';
        $this->baseUrl = Yii::app()->getAssetManager()->publish($dir);

        $cs = Yii::app()->getClientScript();
        $cs->registerScriptFile($this->baseUrl.'/xheditor-'.$this->language.'.min.js');


        $activeId = CHtml::activeId($this->model, $this->attribute);
        $cs->registerScript($activeId.'-Script',
            '$("#'.$activeId.'").xheditor('.$options.');', CClientScript::POS_END);

    }

    public function run()
    {
        $activeId = CHtml::activeId($this->model, $this->attribute);
        echo EHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
    }
}